<?php
/**
*  Project Name:  NET114.COM
*  @author  :  邝达峰
*  @date    : 2014.09.22
*  后台管理基类
*/

abstract class AdminManage extends BaseAction
{
	public $adminid;
       	
	/** 
	* 构造函数
	* @param:  NULL
	* @return: NULL
	* @access: public
	*/
	public function __construct()
	{
		parent::__construct();
		//判断系统运行模式
		$this->checkRunMode();
		//判断用户是否登录
		
		$this->adminid = $this->pVars['adminid'] = $this->checkLoginAdmin();
		if(!$this->adminid) {
			$this->alertGoto('请先登录！','/admins/login.html');
		}
		//判断用户权限
		$this->checkAuthority();
		
		//审核状态
		$this->pVars['checkStateList'] = array('<font color="#000">待审核</font>', '<span style=\'color:#ff0000\'>未通过</span>', '<span style=\'color:#009900\'>已通过</span>','<font color="#ff0000">已删除</font>');
		
		$this->system_log();//操作记录
		
		//初始化菜单
		$this->initMenuList();
		
	}

	/**
	 * 检查运行模式
	 * 用于防止跨正式环境与测试环境之间的恶意切换
	 * @author 邝达峰
	 */
	public function checkRunMode(){
		#判断系统运行模式
		$intRunMode = $this->session()->getSession('system_run_mode');
		#模式不一样
		if($intRunMode!=SYSTEM_RUM_MODE){
			session_unset();
			session_destroy();
			setcookie('adid', '', -1, '/admins/');
		}
	}


	/**
	 * 检查是否登录
	 * @author 邝达峰
	 */
	protected function checkLoginAdmin(){
		//获取系统后台id
		$adminid = $this->session()->getSession('ADMINID');
		//后台数据库操作对象
		$objAdmins = $this->getModel('admins');
		#判断登录session为空，但是cookie没有为空
		if(empty($adminid) && isset($_COOKIE['adid']))
		{
			$adid = Generator::logDeCode($_COOKIE['adid']);
			$adid = intval($adid);
			$arrUserInfo = $objAdmins->getUserInfoById($adid);
			if(count($arrUserInfo) == 1)
			{
				$this->session()->writeSession($arrUserInfo[0]);
			}
		}
		if($adminid) {
			$arrUserInfo = $objAdmins->getUserInfoById($adminid);
			if($arrUserInfo){
				$arrUserInfo = $arrUserInfo[0];
				$this->assign('loginUserInfo',$arrUserInfo);
			}else{
					session_start();
					session_unset();
					session_destroy();
					setcookie('adid', '', -1, '/admins/');
					$this->alertGoto('账号异常,请跟管理员联系！','/admins/login.html');
			}
			return $adminid;
		}else{
		    return false;
		}
	}


	/*
	* 当前管理员的权限判断
	*/
	protected  function checkAuthority()
	{
		if($_REQUEST['module'] == 'admins' &&$_REQUEST["action"]=='index' ){
				return true;
		}
		$intMenuId = $this->session()->getSession('MENUID');
		$arrSpecialAction = array("read_pwd","productedit","product_modify_edit","modifyedit","member_modify_ajax","adminadd");
		if(!empty($intMenuId)){
			$sql = "select 1 CNT from menu where  menuid in(".$intMenuId.") and action like '".$_REQUEST["action"]."%' and  isShow=1";
			$acc_menu = DB::Select($sql,-1);
			//权限判断
			if(!in_array(strtolower($_REQUEST["action"]),$arrSpecialAction)) {
				if(!$acc_menu){
					exit("无权限访问此页");
				}
			}
		}else {
			$this->alertGoto('请先登录！','/admins/login.html');
		}
	}
    
    /*
	* 后台常用变量参数
	*/
    protected function getConfigParams(){
        $res['system_groups'] = array(0=>'行政部',1=>'产品部',2=>'企划部',3=>'商务合作部',4=>'客服部',5=>'销售部',6=>'运营部',7=>'售后');
        //crm小组架构
        $res['crm_groups'] = array(1=>'销售一组',2=>'销售二组',3=>'销售三组',10=>'销售个人组');
        //crm用户级别
        $res['crm_grades'] = array(10=>'普通成员',20=>'小组主管',90=>'超级管理员');
        return $res;
    }
	
	
	
	
	/**
	 * 后台操作记录日志
	 * @param string $type 操作类型
	 * @param string $module 操作模块
	 * @param string $remark 操作备注
	 * @return bool
	 * @access public
	 * @author 张伟杰
	 */
	public function system_log($type='',$module='',$remark='')
	{
		if($module == 'treelist' || $_GET['action'] == 'treelist') return '';
		if(empty($type))
		{
			if($GLOBALS['pVars']['subtitle'])
			{
				$type = ($end = strpos($GLOBALS['pVars']['subtitle'],'-'))?substr($GLOBALS['pVars']['subtitle'],0,$end):$GLOBALS['pVars']['subtitle'];
			}
			elseif($GLOBALS['pVars']['menutitle'])
			{
				$type = ($end = strrchr($GLOBALS['pVars']['menutitle'],'>'))?substr($end,1):$GLOBALS['pVars']['menutitle'];
				$type = ($end = strpos($GLOBALS['pVars']['menutitle'],'-'))?substr($GLOBALS['pVars']['menutitle'],0,$end):$GLOBALS['pVars']['menutitle'];
			}
			else $type = DB::select("select menuName CNT from menu where action = '{$_GET['action']}'",-1);
		}
		
		$insert['type'] = $type?$type:$_GET['action'];
		$insert['module'] = $module?$module:$_GET['action'];
		if(empty($remark) && ($mid = DB::select("select menuId CNT from menu where action = '{$_GET['action']}'",-1)))
		{
			$remark = DB::select('select menuName CNT from menu where menuId = '.substr($mid,0,3),-1).' > '.$type;
		}
		
		$insert['remark'] = $remark;
		$insert['url'] = $_SERVER['REQUEST_URI'];
		$insert['method'] = $_SERVER['REQUEST_METHOD'];
		$insert['dateline'] = time();
		$insert['checker'] = $this->adminid;
		return DB::insert('system_log',$insert);
	}
	
	/**
	**初始化菜单
	**/
	public function initMenuList(){
		$strSessionMenu = $this->session()->getSession('MENUID');
		if(empty($strSessionMenu))
		{
			$this->assign('menuChildList','');
		    $this->assign('menuParentList','');
			$this->assign('menuonecount','');
		}
		#创建数据库操作对象
		$objAdmins = $this->getModel('admins');
		$arrData =$objAdmins->getMenuById($strSessionMenu,1);
		if($arrData){
			$arrMenuChildList = array();
			$arrCheckChild = array();
			
			foreach($arrData as $value)
			{
				if(strlen($value['MENUID']) > 3){
				  if(!in_array($value['MENUID'],$arrCheckChild)){
					#获取子菜单
					$arrMenuChildList[substr($value['MENUID'],0,3)][] = $value;
					array_push($arrCheckChild,$value['MENUID']);
				  }
				  $pMenuId[] = substr($value['MENUID'],0,3);
			   }
			}
			#获取父菜单
			$strpMenuId = implode(',',array_unique($pMenuId));
			$arrMenuList =$objAdmins->getMenuById($strpMenuId,1);
		    $this->assign('menuChildList',$arrMenuChildList);
		    $this->assign('menuParentList',$arrMenuList);
			$this->assign('menuonecount',"'".count($pVars['menuParentList'])."'");
		}
	}
	
	/**
	**获取到部门信息
	@param $id 根据ID获取
	*/
	public function get_department_info($id=''){
		if(!empty($id)){
			$sql = 'select id,groupName from system_group where id = '.$id.' limit 1 ';
		}else{
			$sql = 'select id,groupName from system_group order by id asc ';
		}
		$arrData =  DB::select($sql);
		return $arrData;
	}
       
        

}
?>